home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / dsp / dspgroup / newspect.arc / SPECT1K.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-04-27  |  8.6 KB  |  370 lines

  1. #include    <stdio.h>
  2. #include    <dos.h>
  3. #include    <memory.h>
  4. #include    <process.h>
  5. #include        <conio.h>
  6. #include    <graph.h>
  7. #define    CONTROL 0x34
  8. #define    CONT2 0xb4
  9. #define COMLINE 62        /*  (f4)  */
  10. #define THRESH 63     /*   (f5)  */
  11. #define UP 72
  12. #define DOWN 80
  13. #define PGUP 73
  14. #define PGDOWN 81
  15. #define RIGHT 77
  16. #define LEFT 75  
  17. #define CNRIGHT 116
  18. #define CNLEFT 115  
  19. #define HOME 71
  20. #define END 79
  21. #define CNHOME 119
  22. #define CNEND 117
  23. #define CLOSE 61         /* (f3)   */
  24. #define TCONSTU 60       /* (f2)  */ 
  25. #define TCONSTD 59       /* (f1)  */ 
  26. #define TCONST_NEW 68    /* (f10) */
  27. #define GO inp(0x306)
  28. #define STOP inp(0x307)
  29. unsigned int seg320,io320,prt0,cntrlpt,prt1,prt2,zl[512];
  30. int spect_count,y[512],zh[512],threshold;
  31. long z[512];
  32. float norm[512],trate,freqmax;
  33. char *adr1, *adr2;
  34. cdecl sigpts();
  35. cdecl main(argc,argv)
  36. int argc;
  37. char *argv[];
  38. {
  39. char c,scr_csts();
  40. int cmin,j,jh,k,i,scale,offset,kount;
  41. int ct,binmax;
  42. long count,jjj,jj,jjmax,jjmore,jjcnt;
  43. float zhold[513],zsum[512],tconst,rate,osc,amplitude;
  44.  
  45. seg320=0xd000;
  46. adr1 = (char *)0xD0001800L;
  47. adr2 = (char *)0xD0001C00L;
  48. tconst=0.5;
  49. jjcnt=0;
  50. io320=0x300;            /*  base i/o address for 320 board */
  51. osc = 25e6/4.;          /*  basic board oscillator */
  52. cntrlpt = io320 + 7;
  53. prt0 = io320 + 4;
  54. prt2 = io320 + 6;
  55. scale = 5;
  56. jjmore = 0;
  57. offset = 180;
  58. clark: do {
  59.     cls();
  60.     printf("Input desired sample rate in Hz ");
  61.     scanf( "%f",&rate);
  62. } while ( (rate < 80.0 ) || (rate > 40000.1));
  63. /* count gives you the oscillator "divisor" just as in SIO's */
  64. count = osc/rate;
  65. cmin = osc /40000.0;
  66. if ( count < 2 ) count = 2;
  67. trate = osc /(float)count;
  68. printf("\nRate = %f ",trate);
  69. /* set timers for desired sampling rate */
  70. control_timer(count,osc,&trate);
  71. _settextposition(24,1);
  72. printf("%7.2f Hz     ",trate);
  73. for(k=0;k<20000;k++) {       /* time delay for 8254 & clear holding arrays */
  74.       
  75.     if (k<513) {
  76.         zhold[k]=0.;
  77.     if (k<512) norm[k]=0.;
  78.     }
  79. }     
  80.  
  81. STOP;   /* halt */
  82. /* Load the fft and square routines */
  83. spawnl(P_WAIT,"intel",argv[0],"ffthy1k","d000","300","0",NULL);
  84. /* Run it once to set up arrays and have proper values in the ports */
  85. GO; /* go */
  86. while(inp(io320)!=88) {};
  87. STOP; /* stop */
  88. /* Generate Reference Baseline Spectrum */
  89.     printf("\n How many samples in reference spectrum? ");
  90.     scanf("%d",&spect_count);
  91.     if (spect_count == 0) {
  92.     for(k=0;k<512;k++) norm[k]=0.05;
  93.     } else ref_spect(spect_count);
  94. STOP;
  95. printf("\nReference spectrum --- Hit any character when ready");
  96. ci();
  97. _setvideomode(_TEXTC80);
  98. _settextposition(1,1);
  99.     printf("\n How many samples for each displayed spectrum? ");
  100.     scanf("%d",&spect_count);
  101.  
  102. /* Set it up to run forever until ^C or F3 to change screen and quit*/
  103. _setvideomode(_HRESBW);
  104. _setviewport(0,8,639,176);
  105. kount=0; 
  106. GO;
  107. while (1) 
  108. {
  109.     if ((c = scr_csts()) != 0 )
  110.         {
  111.          switch(c) {
  112.              case COMLINE: {
  113.                  _setvideomode(_TEXTC80);;
  114.                  goto clark;
  115.                 break;
  116.             }
  117.             case THRESH: {
  118.                 _setvideomode(_TEXTC80);;
  119.                 printf("Input threshold %d? ",threshold);
  120.                 scanf("%d",&threshold);
  121.                 if (threshold < 1) threshold=0;
  122.                 if (threshold > 199) threshold = 199;
  123.                 if (threshold==0) outp(97,inp(97)&0xFC);
  124.                 _setvideomode(_HRESBW);
  125.                 _setviewport(0,8,639,176);
  126.                 break;
  127.             }
  128.             case UP: {
  129.                 scale -= 1;
  130.                 if (scale < 0) scale = 0;
  131.                 break;
  132.             }
  133.             case DOWN: {
  134.                 scale += 1;
  135.                 if (scale > 15) scale = 15;
  136.                 break;
  137.             }
  138.             case PGUP: {
  139.                 offset -= 10;
  140.                 if (offset < 0) offset = 0;
  141.                 break;
  142.             }
  143.             case PGDOWN: {
  144.                 offset += 10;
  145.                 if (offset > 180) offset = 180;
  146.                 break;
  147.             }
  148.             case CLOSE: {
  149.                 STOP; /* halt */
  150.                 outp(97,inp(97)&0xFC);
  151.                 _setvideomode(_TEXTC80);;
  152.                 exit();
  153.                 break;
  154.             }
  155.             case HOME: {
  156.                 jjmore -= 1;
  157.                 if (jjmore < 0) jjmore = 0;
  158.                 break;
  159.             }
  160.             case END: {
  161.                 jjmore += 1;
  162.                 if (jjmore > 10000) jjmore = 10000;
  163.                 break;
  164.             }
  165.             case CNHOME: {
  166.                 jjmore /= 2;
  167.                 if (jjmore < 0) jjmore = 0;
  168.                 break;
  169.             }
  170.             case CNEND: {
  171.                 jjmore *= 2;
  172.                 if (jjmore > 10000) jjmore = 10000;
  173.                 break;
  174.             }
  175.                  case TCONSTU: {
  176.                 tconst *= 2.0;
  177.                 if (tconst>1.0) tconst=1.0;
  178.                 break;
  179.             }   
  180.             case TCONST_NEW: {
  181.                 _setvideomode(_TEXTC80);; printf("%3d\n",c);
  182.                 printf(" tconst currently = %9.7f --- Enter new value: ",tconst);
  183.                 scanf("%f",&tconst); cls();
  184.                 if (tconst>  1.0) tconst= 1.0;
  185.                 if (tconst<.0001) tconst=.0001 ;
  186.                 _setvideomode(_HRESBW);
  187.                 _setviewport(0,8,639,176);
  188.                 break;
  189.             }
  190.                  case TCONSTD: {
  191.                 tconst *= 0.5;
  192.                 if (tconst<.0001) tconst=.0001 ;     
  193.                 break;
  194.             }   
  195.             default:
  196.                 break;
  197.         }
  198.     }
  199.  
  200. if (z[100]+z[175]+z[200]+z[250]+z[300]+z[400] > (1<<6))  /* is there data? */
  201.   {          /* bypass the following loop if there isn't */
  202. for (k=0;k<512;k++) {
  203.          if (kount == 0) {
  204.             zsum[k]=z[k];
  205.         }
  206.         else zsum[k] += z[k];
  207. }    
  208. kount++;
  209. if (kount >= spect_count)   {   
  210.         kount=0;
  211.     for (k=0;k<512;k++) zsum[k] *= norm[k];        
  212.         amplitude=0.;
  213.         binmax=128;
  214.     for(k=1;k<511;k++)        {
  215.         zhold[k]=(1.0-tconst)*zhold[k] +
  216.           (zsum[k]*2.0+zsum[k-1]+zsum[k+1])*0.25*tconst;
  217.         y[k]= offset-(((int)zhold[k])>>scale);
  218.             zsum[k] = 0;
  219.             if (y[k] < 1) y[k]=1;                  /* don't go off the top */
  220.         if (k > 80 && zhold[k]>amplitude) {    /* avoid DC peak */
  221.             binmax=k;
  222.             amplitude=zhold[k];
  223.         }
  224.     }
  225.         _settextposition(24,1);
  226.         freqmax=((float)binmax)*trate/1024.0;
  227.     printf("Rate= %5.0f Hz Scale= %d tconst= % 5.4f sampl= %d freq=% 5.0f amp=% 4.0f  ",
  228.             trate,scale,tconst,spect_count,freqmax,amplitude);
  229.     sigpts(&y[0],512);      /* show old fft */
  230.     if (threshold!=0){
  231.         if (y[binmax]<threshold) {
  232.             outp(97,inp(97)&0xFC);
  233.             prt1=1193180.0/freqmax;
  234.             outp(67,182);
  235.             outp(66,prt1&0xFF);
  236.             outp(66,(prt1>>8)&0xFF);
  237.             outp(97,inp(97)|3);
  238.     }
  239.     else outp(97,inp(97)&0xFC);
  240.     }
  241.  
  242.    }
  243.   }     /* end of bypass loop -- display counter even if no data at input */
  244.       
  245.         _settextposition(1,1);  printf("% 5d   ",jjcnt++);
  246.         if (jjcnt>32675) jjcnt=0;
  247.  
  248.     while(inp(io320)!=88) {};
  249.     STOP;  /* halt */
  250.     memcpy((char *)zh,adr1,1024);
  251.     memcpy((char *)zl,adr2,1024);
  252.     GO; /* go */
  253.     for(k=0;k<512;k++) {
  254.         z[k]=zh[k];
  255.         z[k]=(z[k]<<16L)+zl[k];
  256.     }
  257.   }
  258. }
  259.  
  260. cls()
  261. {
  262.     printf("\x1B[2J");
  263. }
  264. /*
  265. cdecl sigpts(y,n)
  266. int *y,n;
  267. {
  268.     int center;
  269.     register int k;
  270.     center = (640-n)/2;
  271.     for(k=0;k<n;k++){
  272.         _moveto(k+center,177);
  273.         _setcolor(1);
  274.         _lineto(k+center,*(y+k));
  275.         _setcolor(0);
  276.         _lineto(k+center,0);
  277.     }
  278. }
  279. */    
  280. ci()
  281. {
  282.     int j;
  283.     while (kbhit()==0) {};
  284.       j=getch();
  285. }
  286.  
  287. char scr_csts()
  288. {
  289.     char c;
  290.     if (kbhit()==0) return(0);
  291.     if (getch()!=0) return(0);
  292.     return(getch());
  293. }
  294.  
  295. control_timer(cnt,osc,trate)
  296. long cnt;
  297. float *trate,osc;
  298. {
  299.     int ctl,cth;
  300.     /* tell board timer info coming */
  301.     outp(cntrlpt ,CONTROL);
  302.     /* count is timing divisor */
  303.     ctl = cnt & 0xff;    /* lo byte */
  304.     cth = ( cnt >> 8 ) & 0xff;/* hi byte */
  305.     /* control timer 0 */
  306.     outp(prt0 , ctl);
  307.     outp(prt0 , cth);
  308.     /* control timer 2 */
  309.     outp(cntrlpt , CONT2);
  310.     outp(prt2 , ctl);
  311.     outp(prt2 , cth);
  312.     /* compute new sampling rate  for output on screen*/
  313.     *trate = osc/(float)cnt;
  314. }
  315. ref_spect(spect_count)
  316. int spect_count;
  317. {
  318. float t1,t2,t3,t4,norm_max;
  319. int binmax,jj,k;
  320.  
  321.         GO; /* go */
  322. for(k=0;k<512;k++) norm[k] = 0.0;
  323. for (jj=0;jj<spect_count;jj++) {
  324.         _settextposition(4,1);printf("% 5d  ",jj);
  325.     while(inp(io320)!=88) {};
  326.     STOP;  /* halt */
  327.     memcpy((char *)zh,adr1,1024);
  328.     memcpy((char *)zl,adr2,1024);
  329.     GO; /* go */
  330.     for(k=0;k<512;k++) {
  331.         z[k]=zh[k];
  332.         z[k]=(z[k]<<16)+zl[k];
  333.     }
  334.         for(k=0;k<512;k++) norm[k] += z[k];
  335. }
  336. binmax=256;
  337. norm_max=-999.;
  338. t1 = t2 = t3 = t4 = 0.0;
  339. for (k=0;k<506;k++) {    /* start with 7-point running average smoothing */
  340.     t4=t3;
  341.     t3=t2;
  342.     t2=t1;
  343.     t1=norm[k]+norm[k+1]+norm[k+2]+norm[k+3]+norm[k+4]+norm[k+5]+norm[k+6];
  344.     norm[k]=t4/7.0;          /* keep centered at proper index */
  345.     
  346.        if ( norm[k] > norm_max ) {
  347.            binmax = k;
  348.            norm_max = norm[k] ;
  349.        }    
  350.    }
  351. for (k=0;k<25;k++) {
  352.        norm[k]=0.;
  353.        norm[511-k]=0.;
  354.      }  
  355. for (k=0;k<512;k++) {
  356.     y[k]=180 - (norm[k]/norm_max)*100.;
  357.     if ((norm[k]>100) && (k>60)) 
  358.                  norm[k] = norm_max/(norm[k]*512.0);
  359.     else         norm[k] = 0.;
  360. }
  361.         _setvideomode(_HRESBW);
  362.         _setviewport(0,8,639,176);
  363.         sigpts(&y[0],510);
  364.            _settextposition(24,1);
  365. printf("Rate = %7.2f Hz                          freq=% 7.1f amp=% 8.1f  ",
  366.             trate,         ((float)binmax)*trate/512.0,norm_max );
  367.  
  368. }
  369.  
  370.